import { type Directive } from 'vue'

//角色权限
export const hasRoles = (roles: string[]) => {
  const roleList: any[] = ['demo']
  return roleList.some((role: any) => {
    return roles.includes(role)
  })
}

/** 权限指令，和权限判断函数 checkPermission 功能类似 */
export const roles: Directive = {
  mounted(el, binding) {
    const { value: permissionRoles } = binding
    if (Array.isArray(permissionRoles) && permissionRoles.length > 0) {
      const hasPermission = hasRoles(permissionRoles)
      // hasPermission || (el.style.display = "none") // 隐藏
      hasPermission || el.parentNode?.removeChild(el) // 销毁
    } else {
      throw new Error(`need roles! Like v-roles="['admin','subAccount']"`)
    }
  }
}
